import com.mysql.cj.jdbc.MysqlDataSource;
import com.mysql.cj.jdbc.exceptions.MySQLQueryInterruptedException;

import javax.sql.DataSource;
import javax.xml.transform.Result;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//
////jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&useSSL=false
//public class TestJDBCSelect {
//    public static void main(String[] args) throws SQLException {
//        // 1.构造 DataSource
//        DataSource dataSource = new MysqlDataSource();
//        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&useSSL=false");
//        ((MysqlDataSource) dataSource).setUser("root");
//        ((MysqlDataSource) dataSource).setPassword("abc123");
//
//        // 2.建立连接
//        Connection connection = dataSource.getConnection();
//
//        // 3.构造 SQL
//        String sql = "select * from student";
//        PreparedStatement statement = connection.prepareStatement(sql);
//
//        // 4.执行sql
//        ResultSet resultSet = statement.executeQuery();
//        while (resultSet.next()) {
//            // 获取"id"这一列的数值,期望得到一个int
//            int id = resultSet.getInt("id");
//            // 获取"name"这一列的数值,期望得到一个Stirng
//            String name = resultSet.getString("name");
//
//            System.out.println("id:" + id + ",name:" + name);
//        }
//
//        // 5.释放资源
//        statement.close();
//        connection.close();
//    }
//}

public class TestJDBCSelect {
    public static void main(String[] args) throws SQLException{
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("abc123");

        Connection connection = dataSource.getConnection();

        String sql = "select * from student";
        PreparedStatement statement = connection.prepareStatement(sql);

        ResultSet resultSet = statement.executeQuery();
        while(resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");

            System.out.println("id:" + id + ",name:"+name);
        }

        statement.close();
        connection.close();
    }
}















